public class SingleList implements ISingleList {


    //每一个结点都有val域和next域，所以我们需要构造结点类来存放
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

    }


    public ListNode head;


    //创建结点
    public void createNode() {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
    }


    //打印结点的val
    public void show() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + "->");
            cur = cur.next;
        }
        System.out.println();
    }


    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        //如果链表本身为空
        if(head == null) {
            head = newNode;
            return;
        }
        newNode.next = head;
        head = newNode;
    }

    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        ListNode cur = head;
        //如果链表本身为空
        if(head == null) {
            head = newNode;
            return;
        }
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = newNode;

    }

    public void addIndext(int indext, int data) {
        //确保插入下标是有效范围内
        if(indext < 0 || indext > size()) {
            return;
        }

        //如果indext == 0 -->头插
        if(indext == 0) {
            addFirst(data);
        } else if(indext == size()) {
            //尾插
            addLast(data);
        }

        ListNode newNode = new ListNode(data);
        ListNode cur = head;
        //找到要插入indext的前一个结点
        for(int i = 1; i < indext; i++) {
            cur = cur.next;
        }
        //出了循环，cur就在要插入的indext的前一个结点
        newNode.next = cur.next;
        cur.next = newNode;

    }

    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;

    }

    public void remove(int key) {
        ListNode cur = head;
        ListNode prv = null;
        while(cur != null && cur.val != key) {
            prv = cur;
            cur = cur.next;
        }

        if(cur == null) {
            System.out.println("没有key结点， key为：" + key);
            return;
        }

        //加入要删除的结点是头结点
        if(cur == head) {
            head = head.next;
            return;
        }

        //出了循环，cur就在第一个key结点
        prv.next = cur.next;

    }

    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }

        ListNode cur = head.next;
        ListNode prv = head;

        while(cur != null) {
            if(cur.val == key) {
                prv.next = cur.next;
                cur = cur.next;
            } else {
                prv = prv.next;
                cur = cur.next;
            }

        }

        //最后判断一下头结点的val是否是key
        if(head.val == key) {
            head = head.next;
        }

    }

    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }


    public void clear() {
        head = null;

    }


}
